import fs from "fs";
import path from "path";
// 创建通用类型
export const FileUtils = {
    createFileNode: (filePath, parent) => {
        const stats = fs.statSync(filePath);
        const isDirectory = stats.isDirectory();
        const name = path.basename(filePath);
        return {
            name,
            path: filePath,
            isDirectory,
            children: [],
            parent,
        };
    },
    // 获取文件夹下的所有文件
    getTreeStructureSync: (dirPath) => {
        try {
            const root = FileUtils.createFileNode(dirPath, null);
            const files = fs.readdirSync(dirPath);
            files.forEach(file => {
                const filePath = path.join(dirPath, file);
                const node = FileUtils.createFileNode(filePath, root);
                if (node.isDirectory) {
                    node.children = FileUtils.getTreeStructureSync(filePath);
                }
                root.children.push(node);
            });

            return root.children;
        } catch (error) {
            console.error(`读取目录时出错: ${dirPath}`, error);
            return [];
        }
    },
    // 文件保存到本地
    saveFileToLocal: (prePath, file) => {
        // 在Electron环境中，你可以使用fs模块来读取和写入文件
        // 这里我们假设你想要将文件保存到应用程序的数据目录下
        const targetPath = path.join(prePath, file.name);

        // 读取文件内容并写入新的位置
        fs.readFile(file.path, (err, data) => {
            if (err) {
                console.error('Error reading file:', err);
                return;
            }

            fs.writeFile(targetPath, data, err => {
                if (err) {
                    console.error('Error saving file:', err);
                }

            });
        });
    },
    createDefaultProject:  (projectPath) => {
        FileUtils.createFolder( path.join(projectPath, 'pages'))
    },
    createFolder: (folderPath) => {
        fs.mkdirSync(folderPath);
    },
    // 创建文件
    createFile:  (projectPath) =>  {
        console.log(projectPath)
    }
};




